
#ifndef   __EXTERNAL_EEPROM_24FC512
#define   __EXTERNAL_EEPROM_24FC512


#define   UNKNOWN_GROUP		0xFFFF

#define   I2C_ERROR	(-1)

#define   PIN_SDA			RC4
#define   PIN_SDA_DIR	TRISC4

#define   PIN_SCL			RC3
#define   PIN_SCL_DIR	TRISC3

#define   EX_ERROR	(-1)

enum
{
	EX_ERROR__WRITE_BYTE = (-10),
	EX_ERROR__READ_BYTE,
	EX_ERROR__DIRECTORY_IS_NOT_EXISTS,
	EX_ERROR__ENTRY_NOT_FOUND,				
	EX_ERROR__WRONG_ENTRY_NAME,				
	EX_ERROR__NO_FREE_ENTRY,
	EX_ERROR__WRONG_INDEX,
	EX_ERROR__INDEX_IS_EMPTY
};

typedef struct {
		unsigned int  DirectioryName;  		/* Address of Directory */
		unsigned int  EntryCount;  			/* Count of Entrys in the Directory (max. size of Directory) */
		unsigned char EntryNameSize;/*Size of Entry Name; use this parameter for Entry search in the Directory */
		unsigned char EntrySize;			  	/* Entry size in bytes */
		void (*Print) ( void );
		//FUNCTION *Print;
}EX_DIRECTORY_PROPERTIES;

enum 
{
	DIRECTORY__OWNERS_LIST = 0,
	DIRECTORY__USER_LIST_1,
	DIRECTORY__USER_LIST_2,
	DIRECTORY__USER_LIST_3,
	DIRECTORY__USER_LIST_4,
	DIRECTORY__INPUTS_DESCRIPTORS_LIST,
	DIRECTORY__RF_REMOTE_LIST,
	DIRECTORY__RF_INPUTS_DESCRIPTORS_LIST,
	DIRECTORY__COMMAND_MACROS,

	DIRECTORIES_COUNT
};

extern EX_DIRECTORY_PROPERTIES Ex_Directory[]; 

extern int Ex_Global;
extern int Ex_Errno;

//extern INP_DES;
//void I2C_Init( void );
void Ex_EEPROM_Init( void );
//extern EX_DIRECTORY_PROPERTIES Ex_Directory[INP_DES.SECTORS_COUNT];

int Ex_ReadByte ( unsigned int Address ); /* if returned (-1) - ERROR */
unsigned char Ex_WriteByte( unsigned int Address, unsigned char Byte );

unsigned char Ex_SaveTo__( unsigned int Address, unsigned char* Buffer, unsigned char ByteCount );

unsigned char Ex_Str_SaveTo( unsigned int Address, unsigned char* Buffer );

unsigned char Ex_Str_LoadFrom( unsigned char* Buffer, unsigned int Address );
unsigned char Ex_LoadFrom__( unsigned char* Buffer, unsigned int Address, unsigned char ByteCount );

unsigned char Ex_SaveTo( unsigned int Address, unsigned char* Buffer, unsigned char ByteCount );
unsigned char Ex_SaveTo__( unsigned int Address, unsigned char* Buffer, unsigned char ByteCount );

unsigned char Ex_LoadFrom( unsigned char* Buffer, unsigned int Address, unsigned char ByteCount );

unsigned char Ex_SaveAndCheck( unsigned int Address, unsigned char* Buffer, unsigned char ByteCount );
unsigned char Ex_ClearAll( void );
unsigned char i2c_Seq_Read_Test( void );

unsigned char Ex_Load_And_Send( unsigned int Address, unsigned int ByteCount, unsigned char CheckSum );
	
unsigned char Ex_LoadFrom__ForTest( unsigned int Address, unsigned int ByteCount );
unsigned char Ex_SaveTo_ForTest( unsigned int Address, unsigned int ByteCount );
unsigned char Ex_Load( unsigned char* Buffer, unsigned int Address );

unsigned char Ex_MemSet( unsigned int Address, unsigned char Fill, unsigned int ByteCount );

// ENTRYs

/* Fill struct (type: EX_DIRECTORY_PROPERTIES) of Directory Properties */
int Ex_Entry_GetDirectoryDescription( unsigned char Directory, EX_DIRECTORY_PROPERTIES* DirectoryProperties );

/* return address of free entry or EX_ERROR*/
int Ex_Entry_FindFree( unsigned char dir );

/* If successful - return Address of deleted Entry if not - EX_ERROR value */
int Ex_Entry_DeleteDirectory( unsigned char  dir );

/* pEntry pointer of Entry data type; if successful - return Address of loaded Entry */
int Ex_Entry_Read( unsigned char Directory, unsigned char* pEntry );

int Ex_Entry_Read_ByIndex( unsigned char Directory, unsigned char* pEntry, unsigned int Index );

/*  Write Entry to EEPROM; if the Entry is exists, then rewrite; Return: Address of recorded Entry or ERRORs*/
int Ex_Entry_Write( unsigned char Directory, unsigned char* Entry );

/* return Entry Address if Entry was found if not - EX_ERROR; BytesCount - how many bytes must be matching */
int Ex_Entry_Find ( unsigned char Directory, unsigned char* EntryName, unsigned char BytesCount );

/* If successful - return Address of deleted Entry if not - EX_ERROR value */
int Ex_Entry_Delete ( unsigned char Directory, unsigned char* EntryName );


unsigned int ListNumber_ToAddress(unsigned char ListNumber );
	
#endif
	
